


package com.wanchuang.qhbb.module.main;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.google.protobuf.InvalidProtocolBufferException;
import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.base.BaseFragment;
import com.wanchuang.qhbb.help.GlideHelper;
import com.wanchuang.qhbb.module.account.AccountActivity;
import com.wanchuang.qhbb.module.account.AccountNewActivity;
import com.wanchuang.qhbb.module.allow.AllowApplyActivity;
import com.wanchuang.qhbb.module.machines.AllowActivity;
import com.wanchuang.qhbb.module.machines.AllowBackActivity;
import com.wanchuang.qhbb.module.machines.MachinesActivity;
import com.wanchuang.qhbb.module.partner.PartnerActivity;
import com.wanchuang.qhbb.module.policy.PolicyOneActivity;
import com.wanchuang.qhbb.module.policy.PolicyShareActivity;
import com.wanchuang.qhbb.module.policy.PolicyThreeActivity;
import com.wanchuang.qhbb.module.policy.PolicyTwoActivity;
import com.wanchuang.qhbb.module.shop.ShopActivity;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.WebApis;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.proto.Audit;
import com.wanchuang.qhbb.proto.Index;
import com.wanchuang.qhbb.proto.MemberOuterClass;
import com.wanchuang.qhbb.proto.Recharge;
import com.wanchuang.qhbb.utils.ValidateUtils;
import com.wanchuang.qhbb.view.BannerImageHoderView;
import com.wanchuang.qhbb.view.ImageNumberView;
import com.wanchuang.qhbb.view.MyScrollview;
import com.wanchuang.qhbb.web.ui.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/2/2 0002.
 */

public class HomeFragment extends BaseFragment {


    private Unbinder unbinder;

    @BindView(R.id.tv_member_name)
    TextView tvMemberName;
    @BindView(R.id.scrollview)
    MyScrollview scrollview;
    @BindView(R.id.rv_title)
    LinearLayout rvTitle;
    @BindView(R.id.home_convenient_banner)
    ConvenientBanner convenientBanner;
    @BindView(R.id.home_convenient_banner_bottom)
    ConvenientBanner convenientBannerBottom;
    @BindView(R.id.tv_marquess)
    TextView tvMarquess;
    @BindView(R.id.iv_image_one)
    ImageView imageViewOne;
    @BindView(R.id.iv_image_two)
    ImageView imageViewTwo;


    @BindView(R.id.v_select_one)
    View v_select_one;

    @BindView(R.id.v_select_two)
    View v_select_two;

    @BindView(R.id.v_select_three)
    View v_select_three;


    @BindView(R.id.tv_select_one)
    TextView tv_select_one;

    @BindView(R.id.tv_select_two)
    TextView tv_select_two;

    @BindView(R.id.tv_select_three)
    TextView tv_select_three;
//
    @BindView(R.id.tv_shou_yi)
    TextView tv_shou_yi;

    @BindView(R.id.tv_jiao_yi)
    TextView tv_jiao_yi;

    @BindView(R.id.tv_ji_huo)
    TextView tv_ji_huo;


//    @BindView(R.id.home_today_action)
//    ImageNumberView home_today_action; // 激活
//    @BindView(R.id.home_today_income)
//    ImageNumberView home_today_income; // 收入
//    @BindView(R.id.home_today_trad)
//    ImageNumberView home_today_trad;  // 交易
//    @BindView(R.id.home_today_fazan)
//    NumberRunningTextView home_today_fazan;  // 交易

    //    @BindView(R.id.vf1)
//    ViewFlipper viewFlipper1;
//    @BindView(R.id.vf2)
//    ViewFlipper viewFlipper2;
//
//    @BindView(R.id.vf3)
//    ViewFlipper viewFlipper3;
//    @BindView(R.id.vf4)
//    ViewFlipper viewFlipper4;
    private List<Index.get_show_list.List> advDatas;
    private List<Index.get_show_list.List> advDatasTwo;
    private List<Index.get_show_list.List> advImages;
    private ArrayList<Recharge.today_earnings.Earningsinfo.List> mData = new ArrayList<>();


    /**
     * 当前显示的是第几页
     */
    private int curIndex = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void initView() {
        tv_select_one.setOnClickListener(v -> {
            v_select_one.setVisibility(View.VISIBLE);
            v_select_two.setVisibility(View.INVISIBLE);
            v_select_three.setVisibility(View.INVISIBLE);
            tv_jiao_yi.setVisibility(View.VISIBLE);
            tv_shou_yi.setVisibility(View.INVISIBLE);
            tv_ji_huo.setVisibility(View.INVISIBLE);
        });

        tv_select_two.setOnClickListener(v -> {
            v_select_two.setVisibility(View.VISIBLE);
            v_select_one.setVisibility(View.INVISIBLE);
            v_select_three.setVisibility(View.INVISIBLE);
            tv_shou_yi.setVisibility(View.VISIBLE);
            tv_jiao_yi.setVisibility(View.INVISIBLE);
            tv_ji_huo.setVisibility(View.INVISIBLE);
        });

        tv_select_three.setOnClickListener(v -> {
            v_select_three.setVisibility(View.VISIBLE);
            v_select_two.setVisibility(View.INVISIBLE);
            v_select_one.setVisibility(View.INVISIBLE);
            tv_ji_huo.setVisibility(View.VISIBLE);
            tv_shou_yi.setVisibility(View.INVISIBLE);
            tv_jiao_yi.setVisibility(View.INVISIBLE);
        });

    }

    private void loadEarn() {
        NetworkManager.INSTANCE.post(Apis.today_earnings, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Recharge.today_earnings datas = Recharge.today_earnings.parseFrom(bytes);
                mData.clear();
                mData.addAll(datas.getEarningsInfo().getListList());
                if (mData.size() >= 3) {

                    tv_jiao_yi.setText(mData.get(1).getToday());
                    tv_shou_yi.setText(mData.get(0).getToday());
                    tv_ji_huo.setText(mData.get(2).getToday());
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
            }
        });
    }


    /**
     * 加载数据项
     */
    private void initBanner() {
        // 加载banner的数据
        Index.get_show_list.Builder builder = Index.get_show_list.newBuilder();
        builder.setType("1");
        NetworkManager.INSTANCE.post(Apis.get_banner, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Index.get_show_list data = Index.get_show_list.parseFrom(bytes);
                advDatas = data.getAdvListList();
                convenientBanner.setPages(new CBViewHolderCreator() {
                    @Override
                    public BannerImageHoderView createHolder(View itemView) {
                        return new BannerImageHoderView(itemView, mContext);
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.item_localimage;
                    }
                }, advDatas);
                convenientBanner.startTurning(6000);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });


        Index.get_show_list.Builder builder2 = Index.get_show_list.newBuilder();
        builder2.setType("6");
        NetworkManager.INSTANCE.post(Apis.get_banner, builder2.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Index.get_show_list data = Index.get_show_list.parseFrom(bytes);


                advDatasTwo = data.getAdvListList();
                advImages = data.getAdvsList();

                GlideHelper.INSTANCE.loadImage(imageViewOne, advImages.get(0).getAdvCode());
                GlideHelper.INSTANCE.loadImage(imageViewTwo, advImages.get(1).getAdvCode());
                convenientBannerBottom.setPages(new CBViewHolderCreator() {
                    @Override
                    public BannerImageHoderView createHolder(View itemView) {
                        return new BannerImageHoderView(itemView, mContext);
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.item_localimage;
                    }
                }, advDatasTwo);
                convenientBannerBottom.startTurning(6000);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });

    }

    private void loadMemberInfo() {
        NetworkManager.INSTANCE.post(Apis.memberDataInfo, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                MemberOuterClass.Member member = MemberOuterClass.Member.parseFrom(bytes);
                if (TextUtils.isEmpty(member.getMemberName())) {
                    tvMemberName.setText(member.getTruename());
                } else {
                    tvMemberName.setText(member.getMemberName());
                }

            }

            @Override
            public void onError(int code, String errorMessage) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initBanner();
        loadEarn();
//        loadMemberInfo();
        loadAllowInfo();
        setMarqueeText();
    }

    private void loadAllowInfo() {
        NetworkManager.INSTANCE.post(Apis.transfer, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Audit.audit_index data = Audit.audit_index.parseFrom(response);
                Log.e("transfer", data.toString());

                // 准备两个List
                List<String> data_one = new ArrayList<>();
                List<String> data_two = new ArrayList<>();
                List<Audit.audit_index.List> result_data = data.getListList();
                if (!ValidateUtils.isValidate(result_data)) {
                    data_one.add("暂无");
                    data_two.add("暂无");
                } else {
                    // 分类入库和出库
                    for (Audit.audit_index.List tempList : result_data) {
                        if (tempList.getOperate().equals("1")) {
                            data_one.add(tempList.getApplyMemberName() + " 申请划入 " + tempList.getMemberName() + " " + tempList.getDes());
                        } else {
                            data_two.add("申请向 " + tempList.getMemberName() + " 划拨 " + tempList.getDes());

                        }
                    }
                    if (data_one.size() == 0) data_one.add("暂无");
                    if (data_two.size() == 0) data_two.add("暂无");
                }
//                setData(data_one,viewFlipper1);
//                setData(data_two,viewFlipper2);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {

            }
        });

        NetworkManager.INSTANCE.post(Apis.policy, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Audit.audit_index data = Audit.audit_index.parseFrom(response);
                Log.e("policy", data.toString());
                // 准备两个List
                List<String> data_one = new ArrayList<>();
                List<String> data_two = new ArrayList<>();
                List<Audit.audit_index.List> result_data = data.getListList();
                if (!ValidateUtils.isValidate(result_data)) {
                    data_one.add("暂无");
                    data_two.add("暂无");
                } else {
                    // 分类入库和出库
                    for (Audit.audit_index.List tempList : result_data) {
                        if (tempList.getOperate().equals("1")) {
                            data_one.add(tempList.getApplyMemberName() + " 申请修改 " + tempList.getMemberName() + " " + tempList.getDes());
                        } else {
                            data_two.add("申请修改 " + tempList.getMemberName() + " " + tempList.getDes());

                        }
                    }
                    if (data_one.size() == 0) data_one.add("暂无");
                    if (data_two.size() == 0) data_two.add("暂无");
                }
//                setData(data_one,viewFlipper3);
//                setData(data_two,viewFlipper4);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {

            }
        });
    }


    @Override
    protected void init() {
    }


    private void setMarqueeText() {
        tvMarquess.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        tvMarquess.setSingleLine(true);
        tvMarquess.setSelected(true);
        tvMarquess.setFocusable(true);
        tvMarquess.setFocusableInTouchMode(true);
        NetworkManager.INSTANCE.post(Apis.system_log, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                MemberOuterClass.System_log data = MemberOuterClass.System_log.parseFrom(response);
                List<MemberOuterClass.System_log.System_log_list> lists = data.getSystemLogListList();
                if (lists.size() > 0)
                    tvMarquess.setText(lists.get(0).getPushTitle() + " " + lists.get(0).getPushMessage());
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                tvMarquess.setText("暂无通知");
            }
        });


    }

    @OnClick({R.id.home_category_one, R.id.home_category_two, R.id.home_category_three,
            R.id.home_category_four, R.id.home_category_five, R.id.home_category_six,
            R.id.home_category_seven, R.id.home_category_eight, R.id.home_category_nine,
            R.id.home_category_ten, R.id.tv_marquess

    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.home_category_one:
                startActivity(AccountNewActivity.class);
                break;
            case R.id.home_category_two:
                startActivity(PartnerActivity.class);
                break;
            case R.id.home_category_three:
                startActivity(ShopActivity.class);
                break;
            case R.id.home_category_four:
                startActivity(MachinesActivity.class);
                break;
            case R.id.home_category_five:
                startActivity(AllowActivity.class);
                break;
            case R.id.home_category_six:
                startActivity(AllowApplyActivity.class);
                break;
            case R.id.home_category_seven:
                startActivity(PolicyOneActivity.class);
                break;
            case R.id.home_category_eight:
                startActivity(AllowBackActivity.class);
                break;
            case R.id.home_category_nine:
                startActivity(PolicyTwoActivity.class);
                break;
            case R.id.home_category_ten:
                WebViewActivity.startActivity(mContext, WebApis.merchantAccess);
                break;
            case R.id.tv_marquess:
                startActivity(MessageActivity.class);
                break;
//            case  R.id.ll_go_allow:
//                startActivity(AllowApplyActivity.class);
//                break;
//            case  R.id.ll_go_shenhe:
//                startActivity(PolicyThreeActivity.class);
//                break;
        }
    }


    private void setData(List<String> datas, ViewFlipper vf) {
        // 删除所有的View
        vf.removeAllViews();
        for (int i = 0; i < datas.size(); i++) {
            TextView tv = new TextView(mContext);
            tv.setTextColor(Color.parseColor("#EFEFEF"));
            tv.setText(datas.get(i));
            vf.addView(tv); //把需要滚动布局添加到ViewFlipper
        }
        if (datas.size() > 1) {
            vf.setInAnimation(mContext, R.anim.vf_in); //设置动画效果
            vf.setOutAnimation(mContext, R.anim.vf_out);
            vf.startFlipping(); //开始滚动
        }
    }


}
